﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint;

namespace WorkflowUtility
{
    public class StartWorkflow
    {
        public static void AllowWorkflow(string siteName, string listName)
        {

            using (SPSite site = new SPSite(siteName))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    SPList list = web.Lists.TryGetList(listName);
                    Guid wfID = new Guid("8be9f154-6ab9-456e-b0f3-6e8f2af3c86a");
                    var wfAssociation = (from SPWorkflowAssociation spwfasso in list.WorkflowAssociations
                               where spwfasso.BaseId == wfID
                               select spwfasso).FirstOrDefault();
                    wfAssociation.TaskListTitle = "ILXWFTask";
                   var aa=  wfAssociation.TaskListContentTypeId ;//= new Guid("0x0108010045052476B5A94E79B31B54AFB8EAC8BB");

                    wfAssociation.Enabled = true;

                    list.WorkflowAssociations.Update(wfAssociation);

                }

            }
        }

        public static void AllowWorkflow( string siteName, string listName, string templateName , string associationName, string taskListName , string historyListName, string associationData)
        {

            using (SPSite site = new SPSite(siteName) )
            {
                using (SPWeb web = site.OpenWeb())
                {

                    SPList list = web.GetList(listName);
                    SPWorkflowAssociation wrkFl = list.WorkflowAssociations[new Guid("8be9f154-6ab9-456e-b0f3-6e8f2af3c86a")];
                    wrkFl.Enabled = true;

                }
                
            }

            /*
                            //get the workflow associated with this doc lib and kick if off manually
                  

            // 1. create an instance of the SPWorkflowAssociation class
            SPWorkflowAssociation workflowAssociation =
              SPWorkflowAssociation.CreateListAssociation(workflowTemplate, associationName, taskList, historyList);

            // 2. set start options
            workflowAssociation.AllowManual = true;
            workflowAssociation.AutoStartChange = false;
            workflowAssociation.AutoStartCreate = false;

            // 3. set additional association options (if any)
            workflowAssociation.AssociationData = associationData;

            // 4. add workflow association to the list
            list.WorkflowAssociations.Add(workflowAssociation);

            // 5. enable workflow association, so it is displayed in the user interface
            workflowAssociation.Enabled = true;
             * */
        }
    }
}
